package com.rongshu.view.penpal.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.home.entity.PenpalHomeResponse;
import com.rongshu.view.penpal.activity.MorePenpalActivity;
import com.rongshu.view.penpal.activity.PenpalDetailActivity;
import com.rongshu.view.penpal.activity.PenpalHomeActivity;

import java.util.List;

/**
 * created by zhengweis on 2018/11/9.
 * description：笔友会首页的 笔友会列表
 */
public class PenpalAdapter extends RecyclerView.Adapter<PenpalAdapter.ViewHolder> {
    private Context context;
    private List<PenpalHomeResponse.ResponseBean.RecommendPenpalwillBean> list;

    public PenpalAdapter(Context context, List<PenpalHomeResponse.ResponseBean.RecommendPenpalwillBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_penpal, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position == 0) {
            holder.leftView.setVisibility(View.VISIBLE);
            holder.penpalName.setText("更多笔友会");
            Glide.with(context).load(R.drawable.more_penpal_icon).into(holder.penpalLogo);
        } else {
            holder.leftView.setVisibility(View.GONE);
            holder.rank.setVisibility(list.get(position).getIsCreator() ? View.VISIBLE : View.GONE);
            String penclubname = list.get(position).getPenclubname();
            if (penclubname.length() > 5) {
                holder.penpalName.setText(penclubname.substring(0, 5) + "...");
            } else {
                holder.penpalName.setText(penclubname);
            }
            GlideUtils.loadImageView(context, holder.penpalLogo, list.get(position).getImg(),
                    R.drawable.penpal_default_logo, R.drawable.penpal_default_logo);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserInfoUtils.getUserInfo() == null) {
                    LoginUtils.toLogin(context);
                    return;
                }
                if (position >= list.size()) return;
                Intent intent = new Intent();
                if (position == 0) {
                    intent.setClass(context, MorePenpalActivity.class);
                    context.startActivity(intent);
                } else {
                    if (list.get(position).getIspublic()) {
                        intent.setClass(context, PenpalDetailActivity.class);
                        intent.putExtra(Constant.PENPAL_ID, list.get(position).getId());
                        context.startActivity(intent);
                    } else {
                        intent.setClass(context, PenpalHomeActivity.class);
                        intent.putExtra(Constant.PENPAL_ID, list.get(position).getId());
                        context.startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView penpalName, rank;
        private ImageView penpalLogo;
        private View leftView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            leftView = itemView.findViewById(R.id.left_view);
            penpalLogo = itemView.findViewById(R.id.penpal_logo);
            penpalName = itemView.findViewById(R.id.penpal_name);
            rank = itemView.findViewById(R.id.rank);
        }
    }
}
